﻿/*************************************
Function：设置窗体
 Version：1.0
    Date：2012-04-28
  Author：rjdong
   Email：ooxxab@gmail.com
 WebSite：http://emake.info
*************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Wind.Compressor.Business;
using Wind.Compressor.Entity;

using Yahoo.Yui.Compressor;
using System.Globalization;

namespace Wind.Compressor
{
    public partial class FormSetting : Form
    {
        public int tabtype = 1;
        public SettingInfo setting = new SettingInfo();

        public FormSetting()
        {
            InitializeComponent();
        }

        private void FormSetting_Load(object sender, EventArgs e)
        {
            try
            {
                this.cbb_CssCompressionType.SelectedIndex = 1;
                this.cbb_JsCompressionType.SelectedIndex = 1;

                if (tabtype == 1)
                    this.tabControl1.SelectedIndex = 2;
                else if (tabtype == 2)
                    this.tabControl1.SelectedIndex = 1;
                else if (tabtype == 3)
                    this.tabControl1.SelectedIndex = 0;

                EncodingInfo[] encodes =Encoding.GetEncodings();
                this.cbb_CssEncodeName.Items.Clear();
                this.cbb_JsEncodeName.Items.Clear();
                this.cbb_HtmlEncodeName.Items.Clear();
                this.cbb_CssEncodeName.Items.Add("保持原文件编码");
                this.cbb_JsEncodeName.Items.Add("保持原文件编码");
                this.cbb_HtmlEncodeName.Items.Add("保持原文件编码");
                foreach (EncodingInfo item in encodes)
                {
                    this.cbb_CssEncodeName.Items.Add(item.DisplayName);
                    this.cbb_JsEncodeName.Items.Add(item.DisplayName);
                    this.cbb_HtmlEncodeName.Items.Add(item.DisplayName);
                }
                initSetting();
            }
            catch (Exception ex)
            {
                LogHelper.Write("Wind.Compressor.FormSetting.FormSetting_Load error", ex);
            }
        }

        private void initSetting()
        {
            try
            {
                setting = Utils.LoadSetting();

                this.txt_Encoding.Text =  Encoding.Default.EncodingName;
                this.txt_CultureInfo.Text = CultureInfo.CurrentCulture.DisplayName;

                if (setting != null)
                {
                    //css
                    this.cbb_CssCompressionType.SelectedIndex = (int)setting.CssCompressionType;
                    if (setting.CssRemoveComments)
                        this.chb_CssRemoveComments.Checked = true;
                    if (setting.CssIsColumnWidth)
                    {
                        this.chb_CssColumnWidth.Checked = true;
                        this.num_CssColumnWidth.Enabled = true;
                    }
                    this.num_CssColumnWidth.Value = setting.CssColumnWidth;
                    if (setting.CssIsVersion)
                    {
                        this.chb_CssVesion.Checked = true;
                        this.txt_CssVesion.Enabled = true;
                    }
                    this.txt_CssVesion.Text = setting.CssVersion;
                    if (setting.CssIsAutoSave)
                    {
                        this.chb_CssIsAutoSave.Checked = true;
                        this.txt_CssAutoSaveEndStr.Enabled = true;
                    }
                    this.txt_CssAutoSaveEndStr.Text = setting.CssAutoSaveEndStr;
                    this.cbb_CssEncodeName.Text = setting.CssEncodeName;
                    
                    //js
                    this.cbb_JsCompressionType.SelectedIndex = (int)setting.JsCompressionType;
                    this.txt_Encoding.Text = setting.JsEncodingName;
                    this.txt_CultureInfo.Text = setting.JsThreadCulture;
                    if (setting.JsIsEvalIgnored)
                        this.chb_JsIsEvalIgnored.Checked = true;
                    if (setting.JsIsObfuscateJavascript)
                        this.chb_JsIsObfuscateJavascript.Checked = true;
                    if (setting.JsIsVerboseLogging)
                        this.chb_JsIsVerboseLogging.Checked = true;
                    if (setting.JsPreserveAllSemicolons)
                        this.chb_JsPreserveAllSemicolons.Checked = true;
                    if (setting.JsDisableOptimizations)
                        this.chb_JsDisableOptimizations.Checked = true;
                    if (setting.JsIsLineBreakPosition)
                    {
                        this.chb_JsLineBreakPosition.Checked = true;
                    }
                    this.num_JsLineBreakPosition.Value = setting.JsLineBreakPosition;
                    if (setting.JsIsVersion)
                    {
                        this.chb_JsVesion.Checked = true;
                        this.txt_JsVesion.Enabled = true;
                    }
                    this.txt_JsVesion.Text = setting.JsVersion;
                    if (setting.JsIsAutoSave)
                    {
                        this.chb_JsAutoSave.Checked = true;
                        this.txt_JsAutoSaveEndStr.Enabled = true;
                    }
                    this.txt_JsAutoSaveEndStr.Text = setting.JsAutoSaveEndStr;
                    this.cbb_JsEncodeName.Text = setting.JsEncodeName;
                   
                    //html
                    if (setting.HtmlIsRemoveSpace)
                    {
                        this.chb_Html_RemoveEmptySpace.Checked = true;
                    }
                    if (setting.HtmlIsCompressCssInStyle)
                    {
                        this.chb_Html_Css.Checked = true;
                    }
                    if (setting.HtmlIsRemoveCssComment)
                    {
                        this.chb_Html_CssComment.Checked = true;
                    }
                    this.cbb_HtmlEncodeName.Text = setting.HtmlEncodeName;                   
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write("Wind.Compressor.FormSetting.initSetting error", ex);
            }
        }

        private void bt_Save_Click(object sender, EventArgs e)
        {
            try
            {
                SettingInfo setting = new SettingInfo();
                //Css
                setting.CssCompressionType = (CssCompressionType)this.cbb_CssCompressionType.SelectedIndex;
                if (this.chb_CssRemoveComments.Checked)
                    setting.CssRemoveComments = true;
                if (this.chb_CssColumnWidth.Checked)
                    setting.CssIsColumnWidth = true;
                setting.CssColumnWidth = (int)this.num_CssColumnWidth.Value;
                if (this.chb_CssVesion.Checked)
                {
                    setting.CssIsVersion = true;
                    setting.CssVersion = this.txt_CssVesion.Text;
                }
                if (this.chb_CssIsAutoSave.Checked)
                {
                    setting.CssIsAutoSave = true;
                    setting.CssAutoSaveEndStr = txt_CssAutoSaveEndStr.Text;
                }
                setting.CssEncodeName = cbb_CssEncodeName.Text;

                //Js
                setting.JsCompressionType = (JavaScriptCompressionType)this.cbb_JsCompressionType.SelectedIndex;
                setting.JsEncodingName = this.txt_Encoding.Text;
                setting.JsThreadCulture = this.txt_CultureInfo.Text;
                if (this.chb_JsIsEvalIgnored.Checked)
                    setting.JsIsEvalIgnored = true;
                if (this.chb_JsIsObfuscateJavascript.Checked)
                    setting.JsIsObfuscateJavascript = true;
                if (this.chb_JsIsVerboseLogging.Checked)
                    setting.JsIsVerboseLogging = true;
                if (this.chb_JsPreserveAllSemicolons.Checked)
                    setting.JsPreserveAllSemicolons = true;
                if (this.chb_JsDisableOptimizations.Checked)
                    setting.JsDisableOptimizations = true;
                if (this.chb_JsLineBreakPosition.Checked)
                    setting.JsIsLineBreakPosition = true;
                setting.JsLineBreakPosition = (int)this.num_JsLineBreakPosition.Value;
                if (this.chb_JsVesion.Checked)
                {
                    setting.JsIsVersion = true;
                    setting.JsVersion = this.txt_JsVesion.Text;
                }
                if (this.chb_JsAutoSave.Checked)
                {
                    setting.JsIsAutoSave = true;
                    setting.JsAutoSaveEndStr = this.txt_JsAutoSaveEndStr.Text;
                }
                setting.JsEncodeName = cbb_JsEncodeName.Text;

                //html
                if (this.chb_Html_RemoveEmptySpace.Checked)
                {
                    setting.HtmlIsRemoveSpace = true;
                }
                if (this.chb_Html_Css.Checked)
                {
                    setting.HtmlIsCompressCssInStyle = true;
                }
                if (this.chb_Html_CssComment.Checked)
                {
                    setting.HtmlIsRemoveCssComment = true;
                }
                setting.HtmlEncodeName = this.cbb_HtmlEncodeName.Text;

                Utils.SaveSetting(setting);

                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Dispose();
            }
            catch (Exception ex)
            {
                LogHelper.Write("Wind.Compressor.FormSetting.bt_Save_Click error", ex);
            }
        }

        private void chb_CssColumnWidth_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chb_CssColumnWidth.Checked)
                this.num_CssColumnWidth.Enabled = true;
            else
                this.num_CssColumnWidth.Enabled = false;
        }

        private void chb_CssVesion_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chb_CssVesion.Checked)
                this.txt_CssVesion.Enabled = true;
            else
                this.txt_CssVesion.Enabled = false;
        }

        private void chb_JsLineBreakPosition_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chb_JsLineBreakPosition.Checked)
                this.num_JsLineBreakPosition.Enabled = true;
            else
                this.num_JsLineBreakPosition.Enabled = false;
        }

        private void chb_JsVesion_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chb_JsVesion.Checked)
                this.txt_JsVesion.Enabled = true;
            else
                this.txt_JsVesion.Enabled = false;
        }

        private void chb_CssIsAutoSave_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chb_CssIsAutoSave.Checked)
                this.txt_CssAutoSaveEndStr.Enabled = true;
            else
                this.txt_CssAutoSaveEndStr.Enabled = false;
        }
       
    }
}
